import { _decorator, Component, find, Node, NodePool, TextAsset } from 'cc';
import { SceneMgr } from '../../Framework/Scripts/Managers/SceneMgr';
import { RandomUtils } from '../../Framework/Scripts/Utils/RandomUtils';
import { GameUtils } from '../../Framework/Scripts/Utils/GameUtils';
import { Debug } from '../../Framework/Scripts/Managers/Debug';
import { EventMgr } from '../../Framework/Scripts/Managers/EventMgr';
import { TimerMgr } from '../../Framework/Scripts/Managers/TimerMgr';
import { HttpUtils } from '../../Framework/Scripts/Utils/HttpUtils';
import { SoundMgr } from '../../Framework/Scripts/Managers/SoundMgr';
import { ResMgr } from '../../Framework/Scripts/Managers/ResMgr';
import { ExcelMgr } from '../../Framework/Scripts/Managers/ExcelMgr';
import { NodePoolMgr } from '../../Framework/Scripts/Managers/NodePoolMgr';
import { WsNetMgr } from '../../Framework/Scripts/Managers/WsNetMgr';
// const { ccclass, property } = _decorator;

// import { PushDownGoods } from "pb";
import { ProtoBufUtils } from './Utils/ProtobufUtils';
import { UIMgr } from '../../Framework/Scripts/Managers/UIMgr';

// @ccclass('GameApp')
export class GameApp extends Component {
    public static Instance: GameApp = null;

    protected onLoad(): void {
        if (GameApp.Instance !== null) {
            this.destroy();
            return;
        }
        GameApp.Instance = this;
    }

    public Init(): void {
        // console.log("GameApp Init");
    }

    public async EnterGame() {
        console.log("====EnterGame #######");
        // 由其它开发者来接管整个游戏项目的代码编写;
        // SceneMgr.Instance.EnterScene("main"); // 如果你把场景当作是ab包的，那么加载不到;
        SceneMgr.Instance.IE_RunScene('main');
        // end

        // 写一些框架的测试代码，方便代码使用常用的一些模块
        // end

        /*
        //测试debug
        Debug.Log("Hellow",'1');
        Debug.Log("Hellow","2");
        Debug.Log("Hellow","3");
        Debug.Error("Hellow",'3');
        Debug.Warning("Hellow","5");
        // end 
        */

        /*
        // 测试事件订阅与发布
        EventMgr.Instance.AddEventListener("GotHead", this.OnGotHead, this);
        EventMgr.Instance.AddEventListener("GotHead", this.OnGotHead2, this);

        EventMgr.Instance.Emit("GotHead", "123456");

        this.scheduleOnce(()=>{
            EventMgr.Instance.RemoveEventListener("GotHead", this.OnGotHead, this);
            EventMgr.Instance.Emit("GotHead", "789JQK");
        }, 5)
        // end
        */

        /* 
        // TimerMgr.Instance.ScheduleOnce(this.OnTimerOnce, this, 1);
        var timerId: number = TimerMgr.Instance.ScheduleWithParam(this.OnTimerRepeat, this, "hello", -1, 1.0, 5);
        TimerMgr.Instance.ScheduleOnceWithParam(this.OnRemoveTimer, this, timerId, 10);
        */

        /*
        // http网络模块处理
        console.log("http begin");
        HttpUtils.Get('https://www.baidu.com/',null,(err,data)=>{
            console.log("http end");

            if(err) {
                console.log(err);
            }
            else {
                console.log(data);
            }
        })
        */

        /* 
        // 声音管理模块的测试
        console.log('=====声音管理模块的测试=======');
        SoundMgr.Instance.SetMusicMute(false);
        SoundMgr.Instance.PlayMusic("background");
        TimerMgr.Instance.ScheduleOnce(()=>{
           SoundMgr.Instance.SetMusicMute(true);
        }, this, 5);
        // end
        */

        /*
         // 测试表格
       var textAsset = await ResMgr.Instance.IE_GetAsset("Datas", "ExcleTest", TextAsset);
       ExcelMgr.Instance.AddTable("ExcleTest", (textAsset as TextAsset).text);
       console.log(ExcelMgr.Instance.GetTableArr("ExcleTest"));
       var item = ExcelMgr.Instance.QueryOne("ExcleTest", "ID", 1003);
       console.log(item);
       */

       /*
        // 测试节点池
        await NodePoolMgr.Instance.AddNodePool('Charactors','Player',10);
        var player = await NodePoolMgr.Instance.GetNodeInPool('Charactors','Player');
        player.setParent(find("PlayerRoot"));
        NodePoolMgr.Instance.DebugNodePoolInfo('Charactors','Player');

        TimerMgr.Instance.ScheduleOnce(()=>{
            NodePoolMgr.Instance.PutNodeInPool('Charactors','Player',player);
            NodePoolMgr.Instance.DebugNodePoolInfo('Charactors','Player');
        },this,5)

        TimerMgr.Instance.ScheduleOnce(async ()=>{
            player = await NodePoolMgr.Instance.GetNodeInPool('Charactors','Player');
            player.setParent(find("PlayerRoot"));
            NodePoolMgr.Instance.DebugNodePoolInfo('Charactors','Player');
        },this,10)
        // end 
        */

        /*
        // 测试websocket
        WsNetMgr.Instance.ConnectToServer();
        // end
        */

        /*
        // protobuf 测试
        var obj: PushDownGoods = PushDownGoods.create();
        obj.goodsId = 1001;
        obj.goodsNum = 2002;
        obj.type = 3003;

        var objBytes = PushDownGoods.encode(obj).finish();
        console.log('=======序列化为字节流==========',objBytes);
        var newObj: PushDownGoods = PushDownGoods.decode(objBytes);
        console.log('=======反序列化为对象==========',newObj);
        // end 

        // Protobuf 封装
        ProtoBufUtils.InitPbToNameMap();
        objBytes = ProtoBufUtils.PbEncode(obj);
        console.log('=======序列化为字节流1==========',objBytes);
        newObj = ProtoBufUtils.PbDecode<PushDownGoods>('PushDownGoods',objBytes);
        console.log('=======反序列化为对象2==========',newObj);
        newObj = (ProtoBufUtils.PbDecodeWithType(PushDownGoods, objBytes));
        console.log('=======反序列化为对象3==========',newObj);
        console.log(newObj);
        // end 
        */

        UIMgr.Instance.IE_ShowUIView('UIGame');

    }

    private OnRemoveTimer(udata: any): void {
        var tiemrId: number = udata;
        TimerMgr.Instance.UnSchedule(tiemrId);
        console.log("OnRemoveTimer", tiemrId);
    }

    private OnTimerOnce(udata: any): void {
        console.log("OnTimerRepeat", udata);
    }

    private OnTimerRepeat(udata: any): void {
        console.log("OnTimerRepeat", udata);
    }

    private OnGotHead(uname: string, udata: any): void {
        console.log("OnGotHead 1", uname, udata, this);
    }

    private OnGotHead2(uname: string, udata: any): void {
        console.log("OnGotHead 2", uname, udata, this);
    }
}


